package com.gaolang.web.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaolang.web.entities.User;
import com.gaolang.web.handler.BusinessException;
import com.gaolang.web.response.Result;
import com.gaolang.web.response.ResultCode;
import com.gaolang.web.service.IUserService;
import com.gaolang.web.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author gaolang
 * @since 2021-02-22
 */
@RestController
@RequestMapping("/user")
@Api(value = "系统用户模块",tags = "系统用户接口")
public class UserController {

    @Autowired
    IUserService userService;

    @GetMapping("/find_all")
    @ApiOperation(value = "用户列表",notes = "查询所有用户信息")
    public Result findUsers(){
        List<User> users = userService.selectAllUsers();
        return Result.ok().data("users",users);
    }

    @GetMapping("/id")
    @ApiOperation(value = "查询单个用户",notes = "通过ID查询对应的用户信息")
    public Result findUserById(Long id){
        User user = userService.findUserById(id);
        if (Objects.isNull(user)){
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }else {
            user.setPassword(null);
            return Result.ok().data("user",user);
        }
    }

    @GetMapping("/findUserList")
    @ApiOperation(value = "用户列表-分页",notes = "查询所有用户信息-分页")
    public Result findUserPage(@RequestParam(required = true,defaultValue = "1")Integer current
    ,@RequestParam(required = true,defaultValue = "6")Integer size){
        Page<User> userPage =  userService.findUserByPage(current,size);
        return Result.ok().data("total",userPage.getTotal()).data("records",userPage.getRecords());
    }

    @PostMapping("/findUserPage")
    @ApiOperation(value = "用户-部门")
    public Result findUser(@RequestParam(required = true,defaultValue = "1")Integer current
            ,@RequestParam(required = true,defaultValue = "6")Integer size,@RequestBody UserVO userVO){
        Page<User> page = new Page<>(current,size);
        IPage<User> userPage = userService.findUserPage(page, userVO);
        return Result.ok().data("total",userPage.getTotal()).data("records",userPage.getRecords());
    }

    @PostMapping("/addUser")
    @ApiOperation(value = "添加用户",notes = "添加用户信息")
    public Result addUser(@RequestBody User user) {
        try {
            userService.addUser(user);
            return Result.ok();
        }catch (Exception e) {
            //打印日志
            return Result.error();
        }
    }


    @PostMapping("/updateUser")
    @ApiOperation(value = "修改用户",notes = "修改用户信息")
    public Result updateUser(@RequestBody User user){
        User user2 = userService.getUserByUsername(user.getUsername());
        user.setId(user2.getId());
        userService.updateById(user);
        return Result.ok();
    }


    @PostMapping("/deleteUser")
    @ApiOperation(value = "删除用户",notes = "删除用户信息")
    public Result deleteUser(Long id){
        boolean t = userService.removeById(id);
        if (t) {
            return Result.ok();
        }else {
            return Result.error();
        }
    }


}
